package com.app.controller;

import com.app.entity.dto.ReadingRecordDTO;
import com.app.entity.dto.AddCommentDTO;
import com.app.service.IHuibenService;
import com.app.vo.CategoryVO;
import com.app.vo.BookTopicVO;
import com.app.vo.BookCollectionVO;
import com.app.vo.BookPageVO;
import com.app.vo.RecentBookVO;
import com.app.vo.BookInfoVO;
import com.app.vo.LikeResponseVO;
import com.app.vo.CollectResponseVO;
import com.app.vo.FollowResponseVO;
import com.app.vo.BookCommentVO;
import com.app.vo.AddCommentResponseVO;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import lombok.RequiredArgsConstructor;

import javax.validation.Valid;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.error;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@RestController
@RequestMapping("/app-api")
@RequiredArgsConstructor
public class HuibenController {

    private final IHuibenService huibenService;

    // 1. 获取分类 lang: en/zh/all (all表示绘本)
    @GetMapping("/bookCategories")
    public CommonResult<List<CategoryVO>> getBookCategories(@RequestParam("lang") String lang) {
        return success(huibenService.getBookCategories(lang));
    }

    // 4. 获取绘本集或绘本列表（绘本集或绘本列表，统一返回 BookTopicVO） 
    @GetMapping("/bookList")
    public CommonResult<List<BookTopicVO>> getBookList(@RequestParam("categoryId") Long categoryId) {
        return success(huibenService.getBookList(categoryId));
    }

    // 8. 获取绘本集详情（含绘本集合）
    @GetMapping("/bookCollections/{collectionId}")
    public CommonResult<BookCollectionVO> getBookCollectionDetail(@PathVariable("collectionId") Long collectionId) {
        return success(huibenService.getBookCollectionDetail(collectionId));
    }

    // 14. 获取绘本页
    @GetMapping("/book/pages/{bookId}")
    public CommonResult<List<BookPageVO>> getBookPages(@PathVariable("bookId") Long bookId) {
        return success(huibenService.getBookPages(bookId));
    }

    // 15. 保存阅读记录
    @PostMapping("/reading-record")
    public CommonResult<Boolean> saveReadingRecord(@RequestBody @Valid ReadingRecordDTO readingRecordDTO) {
        return success(huibenService.saveReadingRecord(readingRecordDTO));
    }

    // 16. 获取最近阅读的绘本
    @GetMapping("/recentBooks")
    public CommonResult<List<RecentBookVO>> getRecentBooks() {
        return success(huibenService.getRecentBooks());
    }

    // 17. 获取绘本基本信息
    @GetMapping("/book/info/{bookId}")
    public CommonResult<BookInfoVO> getBookInfo(@PathVariable("bookId") Long bookId) {
        return success(huibenService.getBookInfo(bookId));
    }

    // 18. 点赞/取消点赞绘本
    @PostMapping("/book/like/{bookId}")
    public CommonResult<LikeResponseVO> toggleBookLike(@PathVariable("bookId") Long bookId) {
        return success(huibenService.toggleBookLike(bookId));
    }

    // 19. 收藏/取消收藏绘本
    @PostMapping("/book/collect/{bookId}")
    public CommonResult<CollectResponseVO> toggleBookCollect(@PathVariable("bookId") Long bookId) {
        return success(huibenService.toggleBookCollect(bookId));
    }

    // 20. 获取绘本评论列表
    @GetMapping("/book/comments/{bookId}")
    public CommonResult<List<BookCommentVO>> getBookComments(@PathVariable("bookId") Long bookId) {
        return success(huibenService.getBookComments(bookId));
    }

    // 21. 新增评论
    @PostMapping("/book/comment/{bookId}")
    public CommonResult<AddCommentResponseVO> addBookComment(@PathVariable("bookId") Long bookId, 
                                                           @RequestBody @Valid AddCommentDTO addCommentDTO) {
        //判断bookId是否为空如果为空就返回错误
        if (bookId == null) {
            return error(400, "bookId不能为空");
        }
        // 设置绘本ID到DTO中
        addCommentDTO.setBookId(bookId);
        return success(huibenService.addBookComment(addCommentDTO));
    }

    // 22. 关注/取消关注用户
    @PostMapping("/user/follow/{userId}")
    public CommonResult<FollowResponseVO> toggleUserFollow(@PathVariable("userId") Long userId) {
        //判断userId是否为空如果为空就返回错误
        if (userId == null) {
            return error(400, "userId不能为空");
        }
        return success(huibenService.toggleUserFollow(userId));
    }
}
